import router from '@system.router'
import prompt from '@system.prompt'
import api from '../http.js'
import wxpay from '@service.wxpay'
import alipay from '@service.alipay'

function getUserMoney (){
    var that = this
    let data = {token:this.token}
    api.api("/get_user_info", data).then(res =>{
       if(res.code == 1){
         that.glod = res.data.balance
         that.red = res.data.red_envelope
        }   
    })
}


async function  fn_recharge() {
    // prompt.showToast({
    //   message: wxpay.getType() 
    // })
    let isLogin = await this.login() ? await this.login() :false
    let that = this
    if(!isLogin){
      prompt.showToast({
        message:"请先登录"
      })
      router.push({
        uri:"/login",
        params:{source:true}
      })
      return false
    }
    let api_data = {
        recharge_id: this.moneyList[this.choose_money].id,
        bid: this.bid
      },
      url = '',
      _this = this

    // 唤起过支付，退出时不弹充值任务提示
    this.is_show_dialog = 1

    if (this.choose_way == 0) {
      url = '/alipay_app'
    } else {
      if (wxpay.getType() == 'APP') {
        url = '/wxpay'
      } else {
        url = '/wxpay_wap'
      }
    }
    this.info = api_data
    console.log(this.info)
    api.api(url, api_data).then(res => {
      // console.log(res)
      prompt.showToast({
        message:'支付中...'
      })
      this.info = res
      if (res.code == 1) {
        if (this.choose_way == 0) {
          // console.log(res.data.data.orderString)
          alipay.pay({
            orderInfo: res.data.data.orderString,
            callback: function(ret) {  
              _this.info = ret
              if (ret.resultStatus == 9000) {
                _this.getUserMoney()
                if(_this.bid != ''){
                  router.back()
                }else{
                  prompt.showToast({
                    message:"充值成功"
                  })
                }
                
              }
            }
          })
        } else {
          this.info = '进来了'
          //  prompt.showDialog({
          //   message: res.data
          // })
          this.useWxpay(res.data)
        }
        this.loginfo = res.data
      } else {
        prompt.showToast({
          message: res.msg
        })
        this.loginfo = res.data
      }
    })
  }

   function useWxpay(res) {
    let payType = wxpay.getType(),
      _this = this
    console.log(payType)
    // payType = 'MWEB'
      _this.info=payType
    if (payType === 'MWEB') {
      // prompt.showToast({message:res.data})
      // _this.info = res.data.param
      prompt.showDialog({
        message:res.data.param
      })
      wxpay.pay({
        // 微信网页支付的prepayId
        prepayid: res.data.prepay_id,
        referer: res.data.referer,
        extra: {
          // 需要将微信返回的MWEB_URL地址配置到这里
          mweb_url: res.data.param
        },
        fail: function(data, code) {
          _this.info = `WX H5 PAY handling fail, code = ${code}`
        },
        cancel: function() {
          _this.info = 'WX H5 PAY handling cancel'
        },
        success: function(data) {
          //H5方式下，支付成功的回调仅仅只是指将订单递交给微信，并不意味着支付已经成功完成
          _this.info = data
          // prompt.showToast({
          //   message: data
          // })
          // router.push({
          //   uri: '/Tabbar',
          //   params: { currentIndex: 2 }
          // })
          setTimeout(()=>{
                prompt.showDialog({
                  title:"提示",
                  message:"请确定微信支付是否成功？",
                  buttons:[{text:"已完成支付",color:"#FC725F"},{text:"支付遇到问题，重新支付",color:'#e5e5e5'}],
                  success:function(res){
                    if(res.index == 0){
                      _this.getUserMoney()
                      if(_this.bid != ""){
                        router.back()
                      }
                    }else{
                      _this.fn_recharge()
                    }
                  },
                  cancel: function() {
                    console.log('handling cancel')
                  },
              })
          },1000)
        }
      })
    } else if (payType === 'APP') {
      
      wxpay.pay({
        //微信 app支付的prepayId
        prepayid: res.param.prepayid,
        extra: {
          app_id: res.param.appid,
          partner_id: res.param.partnerid,
          package_value: res.param.package,
          nonce_str: res.param.noncestr,
          time_stamp: res.param.timestamp,
          order_sign: res.param.sign
        },
        fail: function(data, code) {
          _this.info = `WX H5 PAY handling fail, code = ${code}`
        },
        cancel: function() {
          _this.info = 'WX H5 PAY handling cancel'
        },
        success: function(data) {
          //H5方式下，支付成功的回调仅仅只是指将订单递交给微信，并不意味着支付已经成功完成
          _this.info = data
          _this.getUserMoney()
            if(_this.bid != ''){
              router.back()
            }else{
              prompt.showToast({
                message:"充值成功"
              })
            }
        }
      })
    } else {
      prompt.showToast({
        message: '不支持微信支付'
      })
      this.info = 'WX PAY is not avaliable'
    }
  }